package com.xiaogao.sys.controller;

import com.xiaogao.common.entity.Result;
import com.xiaogao.common.utils.tree.TreeUtils;
import com.xiaogao.sys.request.perm.AddParam;
import com.xiaogao.sys.request.perm.UpdateParam;
import com.xiaogao.sys.response.SysPermResponse;
import com.xiaogao.sys.service.SysPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/perm")
public class SysPermController {
    @Autowired
    private SysPermService sysPermService;

    @GetMapping("/query/select")
    public Result<Object> querySelect(){
        // 先填充create update user
        List<SysPermResponse> sysPermResponseList =  sysPermService.querySelect();
        List<SysPermResponse> tree = TreeUtils.convertToTree(sysPermResponseList);
        return Result.success(tree);
    }

    @PostMapping("/add")
    public Result<Object> add(@RequestBody AddParam addParam){
        sysPermService.add(addParam);
        return Result.success();
    }

    @PutMapping("/update")
    public Result<Object> update(@RequestBody UpdateParam updateParam){
        sysPermService.update(updateParam);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result<Object> add(@PathVariable String id){
        sysPermService.deleteById(id);
        return Result.success();
    }
}
